import React, { Component } from "react";

const ThemeContext = React.createContext();
const MsgContext = React.createContext();

class Son extends Component {
  render() {
    return (
      <div>
        <h2>Son</h2>
        <ThemeContext.Consumer>
          {(theme) => {
            return (
              <MsgContext.Consumer>
                {(msg) => {
                  return (
                    <div>
                      {theme} --- {msg}
                    </div>
                  );
                }}
              </MsgContext.Consumer>
            );
          }}
        </ThemeContext.Consumer>
      </div>
    );
  }
}

class Parent extends Component {
  render() {
    return (
      <div>
        <h2>parent</h2>
        <Son></Son>
      </div>
    );
  }
}

export default class App extends Component {
  state = {
    msg: "app组件的数据",
    theme: "red",
  };
  render() {
    return (
      <div>
        <h1>app</h1>
        <ThemeContext.Provider value={this.state.theme}>
          <MsgContext.Provider value={this.state.msg}>
            <Parent></Parent>
          </MsgContext.Provider>
        </ThemeContext.Provider>
      </div>
    );
  }
}
